/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.io.IOException;
import java.util.List;
import javax.faces.context.FacesContext;
import model.dao.TaskDao;
import model.domain.Product;
import model.domain.Task;

/**
 *
 * @author damile
 */
public class taskBean {

    public taskBean() {
        this.newtask = new Task();
        this.selectedtask = new Task();
    }

    private Product product;
    private Task newtask;
    private Task selectedtask;
    private List<Task> tasks;

    public Product getProduct(){
        return this.product;
    }

    public void setProduct(Product object){
        this.product = object;
    }

    public Task getNewtask(){
        return this.newtask;
    }

    public void setNewtask(Task object){
        this.newtask = object;
    }

    public Task getSelectedtask(){
        return this.selectedtask;
    }

    public void setSelectedtask(Task object){
        this.selectedtask.setTaskID(object.getTaskID());
        this.selectedtask.setStatus(object.getStatus());
        this.selectedtask.setDescription(object.getDescription());
        this.selectedtask.setUser(object.getUser());
        this.selectedtask.setStart(object.getStart());
        this.selectedtask.setEnd(object.getEnd());
        this.selectedtask.setEstimatedEffort(object.getEstimatedEffort());
        this.selectedtask.setComments(object.getComments());
        this.selectedtask.setProductbacklog(object.getProductbacklog());
    }

    public List<Task> getByproduct(){

        TaskDao home = new TaskDao();
        this.tasks = home.findByProduct(this.product);

        return this.tasks;
    }

    public void submitCreate() throws IOException{

        TaskDao home = new TaskDao();

        Task auxTask = new Task();
        auxTask.setDescription(this.newtask.getDescription());
        auxTask.setProductbacklog(this.product);
        auxTask.setComments(this.newtask.getComments());
        auxTask.setStatus(this.newtask.getStatus());
        auxTask.setEnd(this.newtask.getEnd());
        auxTask.setEstimatedEffort(this.newtask.getEstimatedEffort());
        auxTask.setStart(this.newtask.getStart());
        auxTask.setUser(this.newtask.getUser());

        home.insert(auxTask);

        this.newtask.setComments("");
        this.newtask.setDescription("");
        this.newtask.setEnd(null);
        this.newtask.setEstimatedEffort(0);
        this.newtask.setStart(null);
        this.newtask.setStatus(null);
        this.newtask.setUser(null);

        FacesContext.getCurrentInstance().getExternalContext().redirect("ManageTasks.xhtml");
    }

    public void submitEdit(){

        TaskDao home = new TaskDao();
        home.insertOrUpdate(this.selectedtask);
    }

    public void submitDelete(){
        TaskDao home = new TaskDao();
        home.delete(this.selectedtask);
    }
}
